<?php

include('../../include/functions.inc.php');
if (!isEmpty(getParam('operation'))) {
    switch (getParam('operation')) {
        case 'save':
            save($db, $user);
            break;
    }
}

function save($db, $user) {
    $invoiceData = array("EFECTIVO" => sqlValue(getParam("EFECTIVO"), "double"),
        "TARJETA" => sqlValue(getParam("TARJETA"), "double"),
        "DEPOSITO" => sqlValue(getParam("DEPOSITO"), "double"),
        "NO_BOLETA" => sqlValue(getParam("NO_BOLETA"), "text"),
        "CHEQUE" => sqlValue(getParam("CHEQUE"), "double"),
        "STATUS" => sqlValue(1, "double"));

    if (doubleval(getParam("CHEQUE") > 0)) {
        saveCheque($db, 
            array(  "FK_FACURA" => getParam("ID"),
                    "NUMERO" => getParam("NOCHEKE"),
                    "BANCO" => getParam("BANCO"),
                    "MONTO" => getParam("TOTAL"),
                    "FECHACOBRO" => getParam("FECHACOBRO"),
                    "CONCEPTO" => getParam("CONCEPTO")));
    }

    $serie = sqlValue(getParam("SERIE"), "text");
    $numero = sqlValue(getParam("NUMERO"), "int");

    $db->query_update("cllg_factura", $invoiceData, "SERIE=$serie AND NUMERO=$numero");

    $res = array("R" => 1);
    $_SESSION['pack'] = serialize($res);
    header('Location: ../../include/transport.php');
}

/*
 * Guarda la informacion de medio de pago con cheque
 */

function saveCheque($db, $valCk) {
    $cheque = array("NUMERO" => sqlValue($valCk["NUMERO"], "text"),
        "FECHA" => sqlValue(date("y-m-d"), "date"),
        "MONTO" => sqlValue($valCk["MONTO"], "double"),
        "BANCO" => sqlValue($valCk["BANCO"], "text"),
        "FK_FACURA" => sqlValue($valCk["FK_FACURA"], "int"),
        "FECHA_COBRO" => sqlValue($valCk["FECHACOBRO"], "date"));

    $db->query_insert("cllg_cheque", $cheque);
}

?>